<?php
/**
 * Trading administration interface, based on ModelAdmin
 * @package trading
 */
class TradingAdmin extends ModelAdmin {

    private static $url_segment = 'trading';
    private static $menu_title = 'Trading';
    private static $menu_icon = 'trading/images/trading-icon.png';

    private static $managed_models = array(
    	'TradeSetting',
    	'TradeCoinAccountAdjustment',
    	'TradeOrder',
    	'TradeHistory',
    	'CompanyTradeRequest'
    );
	
	public $showImportForm = false;
	
	public function getEditForm($id = null, $fields = null) {
        $list = $this->getList();
        $exportButton = new GridFieldExportButton('buttons-after-left');
        $exportButton->setExportColumns($this->getExportFields());
        
		if(preg_match("/adjustment/i", $this->modelClass)){
            $listField = GridField::create(
                $this->sanitiseClassName($this->modelClass),
                false,
                $list,
                $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
                    ->removeComponentsByType('GridFieldFilterHeader')
					->removeComponentsByType('GridFieldEditButton')
                    ->addComponents(new GridFieldButtonRow('after'), new GridFieldPrintButton('buttons-after-left'), $exportButton)
            );
        }
		else if($this->modelClass == 'TradeOrder' || $this->modelClass == 'TradeHistory' || $this->modelClass == 'CompanyTradeRequest') {
	        $listField = GridField::create(
	            $this->sanitiseClassName($this->modelClass),
	            false,
	            $list,
	            $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
	                ->removeComponentsByType('GridFieldFilterHeader')
	                ->removeComponentsByType('GridFieldEditButton')
	                ->addComponents(new GridFieldViewButton(), new GridFieldButtonRow('after'), new GridFieldPrintButton('buttons-after-left'), $exportButton)
	        );
		}
		else {
	        $listField = GridField::create(
	            $this->sanitiseClassName($this->modelClass),
	            false,
	            $list,
	            $fieldConfig = GridFieldConfig_RecordEditor::create($this->stat('page_length'))
	                ->removeComponentsByType('GridFieldFilterHeader')
	                ->addComponents(new GridFieldButtonRow('after'), new GridFieldPrintButton('buttons-after-left'), $exportButton)
	        );
		}
        
        // Validation
        if(singleton($this->modelClass)->hasMethod('getCMSValidator')) {
            $detailValidator = singleton($this->modelClass)->getCMSValidator();
            $listField->getConfig()->getComponentByType('GridFieldDetailForm')->setValidator($detailValidator);
        }

        $form = CMSForm::create( 
            $this,
            'EditForm',
            new FieldList($listField),
            new FieldList()
        )->setHTMLID('Form_EditForm');
		$form->setResponseNegotiator($this->getResponseNegotiator());
        $form->addExtraClass('cms-edit-form cms-panel-padded center');
        $form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
        $editFormAction = Controller::join_links($this->Link($this->sanitiseClassName($this->modelClass)), 'EditForm');
        $form->setFormAction($editFormAction);
        $form->setAttribute('data-pjax-fragment', 'CurrentForm');

        $this->extend('updateEditForm', $form);
        
        return $form;
    }

	public function getManagedModels() {
		$models = $this->stat('managed_models');
		if(is_string($models)) {
			$models = array($models);
		}
		if(!count($models)) {
			user_error(
				'ModelAdmin::getManagedModels(): 
				You need to specify at least one DataObject subclass in public static $managed_models.
				Make sure that this property is defined, and that its visibility is set to "public"', 
				E_USER_ERROR
			);
		}

		// Normalize models to have their model class in array key
		foreach($models as $k => $v) {
			if(is_numeric($k)) {
				if(!singleton($v)->canView() && Member::currentUserID()){
					unset($models[$k]);
					continue;
				}
				$models[$v] = array('title' => singleton($v)->i18n_singular_name());
				unset($models[$k]);
			}
			else{
				if(!singleton($k)->canView() && Member::currentUserID()){
					unset($models[$k]);
					continue;
				}
			}
		}
		
		return $models;
	}
}
?>